#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFileDialog>
#include <vector>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_openButton_clicked()
{
    QString fileName = QFileDialog::getOpenFileName(this,
           tr("Open Image"), ".", tr("Image Files (*.png *.jpg *.bmp)"));
       m_originalImg = cv::imread(fileName.toUtf8().data());
       if(m_originalImg.data) {
           this->displayImage(m_originalImg);
           ui->processButton->setEnabled(true);
       }
}


void MainWindow::on_processButton_clicked()
{
    cv::Mat image_CV_8U, color_dst;
    cv::cvtColor(m_originalImg, image_CV_8U, CV_BGR2GRAY);



    double cmin;
    QString cmin_string = ui->cmin_lineEdit->text();
    cmin = cmin_string.toDouble();

    double cmax;
    QString cmax_string = ui->cmax_lineEdit->text();
    cmax = cmax_string.toDouble();

    cv::Size ksize(5,5);
    cv::blur(image_CV_8U,image_CV_8U, ksize);
    cv::Canny( image_CV_8U, image_CV_8U, cmin, cmax, 5, true);
    //cv::threshold(image_CV_8U,image_CV_8U, 200.0, 255.0, CV_THRESH_BINARY_INV);
    cv::cvtColor( image_CV_8U, color_dst, CV_GRAY2BGR );

    double accuRes;
    QString accuRes_string = ui->accuRes_lineEdit->text();
    accuRes = accuRes_string.toDouble();

    double accuThres;
    QString accuThres_string = ui->accuThres_lineEdit->text();
    accuThres = accuThres_string.toDouble();

    double maxLineGap;
    QString maxLineGap_string = ui->maxLineGap_lineEdit->text();
    maxLineGap = maxLineGap_string.toDouble();

    double minLineLength;
    QString minLineLength_string = ui->minLineLength_lineEdit->text();
    minLineLength = minLineLength_string.toDouble();

    std::vector<cv::Vec4i> lines;
    cv::HoughLinesP(image_CV_8U, lines, accuRes, CV_PI/180, accuThres, minLineLength, maxLineGap);

    cv::Scalar color = cv::Scalar(0,0,255);
    std::vector<cv::Vec4i>::const_iterator it2 = lines.begin();
    while(it2!=lines.end()) {
        int x0 = (*it2)[0];
        int y0 = (*it2)[1];
        int x1 = (*it2)[2];
        int y1 = (*it2)[3];
        cv::Point pt1(x0,y0);
        cv::Point pt2(x1,y1);
        cv::line(m_originalImg, pt1, pt2, color);
        ++it2;
    }

    cv::namedWindow( "Canny", CV_WINDOW_AUTOSIZE);
    cv::imshow( "Canny", image_CV_8U);
    cv::namedWindow( "Hough", CV_WINDOW_AUTOSIZE);
    cv::imshow( "Hough", m_originalImg);
}

void MainWindow::displayImage(cv::Mat image)
{
    // Erzeuge ein Qt image
    //QImage img = QImage((uchar*) image.data, image.cols, image.rows, image.step, QImage::Format_Indexed8);
    QImage img = QImage((uchar*) image.data, image.cols, image.rows, image.step, QImage::Format_RGB888);//.scaledToWidth(ui->processImgLabel->width());

    if(ui->processImgLabel) {
        // Erzeuge eine Pixmap
        QPixmap pixmap = QPixmap::fromImage(img);

        // Zeige Pixmap im Label an
        ui->processImgLabel->setPixmap(pixmap);

        // Groessenanpassung des Labels fuer das Bild durchfuehren
        ui->processImgLabel->resize(ui->processImgLabel->pixmap()->size());
    }
}
